package cn.edu.ecut.socket;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;

public class FirstServer {

	public static void main(String[] args) throws IOException {
		
		// 创建一个可以对外提供服务的对象(ServerSocket实例)
		ServerSocket ss = new ServerSocket(); // IOException
		
		byte[] bytes = { 1 , 0 , 0 , 99 };
		// 根据字节数组返回IP地址对应的InetAddress实例
		InetAddress address = InetAddress.getByAddress( bytes ); // UnknownHostException
		final int port = 5555 ; // 指定端口号
		SocketAddress endpoint = new InetSocketAddress( address , port) ;
		// 为服务绑定 IP地址和端口
		ss.bind( endpoint ); // IOException

		System.out.println( ss );
		
		System.out.println( "准备监听客户端连接" );
		// 监听来自客户端的连接
		Socket s = ss.accept();
		System.out.println( "监听到客户端: " + s );
		
		// 关闭服务
		ss.close();

	}

}
